def align_media(video_data, subtitle_duration, mode="individual"):
    aligned_clips = []

    for item in video_data:
        # 计算素材的时长
        media_duration = (item['end_time'] - item['begin_time']) / 1000  # 转换为秒

        # 根据模式处理
        if mode == "individual":
            if media_duration > subtitle_duration:
                # 素材时长大于字幕长度
                speed = subtitle_duration / media_duration
                aligned_clip = {
                    "MediaURL": item['Material_url'],
                    "Speed": speed,
                    "X": 0.25,
                    "Y": 0.25,
                    "Width": 0.5,
                    "Height": 0.5,
                    "In": item['begin_time'] / 1000,
                    "Out": (item['begin_time'] + subtitle_duration * 1000) / 1000,
                    "TimelineIn": item['begin_time'] / 1000,
                    "TimelineOut": (item['begin_time'] + subtitle_duration * 1000) / 1000,
                }
            else:
                # 素材时长小于字幕长度
                aligned_clip = {
                    "MediaURL": item['Material_url'],
                    "Speed": 1.0,
                    "X": 0.25,
                    "Y": 0.25,
                    "Width": 0.5,
                    "Height": 0.5,
                    "In": item['begin_time'] / 1000,
                    "Out": (item['begin_time'] + media_duration * 1000) / 1000,
                    "TimelineIn": item['begin_time'] / 1000,
                    "TimelineOut": (item['begin_time'] + media_duration * 1000) / 1000,
                }
        elif mode == "all":
            # 全部素材模式
            speed = subtitle_duration / media_duration
            aligned_clip = {
                "MediaURL": item['Material_url'],
                "Speed": speed,
                "X": 0.25,
                "Y": 0.25,
                "Width": 0.5,
                "Height": 0.5,
                "In": item['begin_time'] / 1000,
                "Out": (item['begin_time'] + subtitle_duration * 1000) / 1000,
                "TimelineIn": item['begin_time'] / 1000,
                "TimelineOut": (item['begin_time'] + subtitle_duration * 1000) / 1000,
            }
        elif mode == "image":
            # 图片素材模式
            aligned_clip = {
                "MediaURL": item['Material_url'],
                "Speed": 1.0,
                "X": 0.25,
                "Y": 0.25,
                "Width": 0.5,
                "Height": 0.5,
                "In": item['begin_time'] / 1000,
                "Out": (item['begin_time'] + subtitle_duration) / 1000,
                "TimelineIn": item['begin_time'] / 1000,
                "TimelineOut": (item['begin_time'] + subtitle_duration) / 1000,
            }

        aligned_clips.append(aligned_clip)

    return aligned_clips


video_data = [
    {"Material_url": "video1.mp4", "begin_time": 0, "end_time": 5000},  # 5秒
    {"Material_url": "video2.mp4", "begin_time": 1000, "end_time": 7000},  # 6秒
    {"Material_url": "image1.jpg", "begin_time": 2000, "end_time": 3000}  # 图片素材
]
subtitle_duration = 4  # 4秒的字幕长度
mode = "individual"    # 处理单个素材

aligned_clips = align_media(video_data, subtitle_duration, mode)
print(aligned_clips)
